package com.authine.cloudpivot.web.api.secondopen.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.authine.cloudpivot.engine.api.facade.BizObjectFacade;
import com.authine.cloudpivot.engine.api.model.runtime.BizObjectCreatedModel;
import com.authine.cloudpivot.engine.api.model.runtime.SelectionValue;
import com.authine.cloudpivot.engine.api.model.runtime.WorkflowInstanceModel;
import com.authine.cloudpivot.web.api.controller.base.BaseController;
import com.authine.cloudpivot.web.api.secondopen.domain.AjaxResult;
import com.authine.cloudpivot.web.api.secondopen.domain.IdList;
import com.authine.cloudpivot.web.api.secondopen.domain.Iusi6cgb;
import com.authine.cloudpivot.web.api.secondopen.modules.*;
import com.authine.cloudpivot.web.api.secondopen.service.AssessManagementService;
import com.authine.cloudpivot.web.api.secondopen.service.PurManagementService;
import com.authine.cloudpivot.web.api.secondopen.utils.PageUtil;
import com.authine.cloudpivot.web.api.utils.StringUtils;
import com.authine.cloudpivot.web.api.view.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 考核管理控制器
 */
@Api(value = "二开接口",tags = "考核管理控制器")
@RestController
@EnableTransactionManagement
@RequestMapping("/api/AssessManagement")
public class AssessManagementController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(AssessManagementController.class);

    @Autowired(required = true)
    private AssessManagementService assessManagementService;

    /**
     * 修改个人考核表状态
     * @return
     */
    @ApiOperation(value = "修改个人考核表状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "idList",value = "ids",required = false,dataType = "char"),
    })
    @Transactional(rollbackFor = Exception.class)
    @RequestMapping("/updateAssessStatus")
    public AjaxResult updateAssessStatus(String objId,String processCode,String status){
        log.info("获取参数:objId={},processCode={}", objId, processCode);
        if (StringUtils.isBlank(objId) || StringUtils.isBlank(processCode)) {
            log.info("参数不完整，直接结束");
            return AjaxResult.error("参数不完整，直接结束");
        }
        BizObjectFacade bizObjectFacade = super.getBizObjectFacade();
        //获取业务数据
        BizObjectCreatedModel bizObject = bizObjectFacade.getBizObject(processCode, objId);
        if (bizObject == null) {
            log.info("业务对象不存在");
            return AjaxResult.error("参数不完整，直接结束");
        }
        String wId = bizObject.getWorkflowInstanceId();
        log.info("TargetOwner:wId={}", wId);
        WorkflowInstanceModel wiModel = super.getWorkflowInstanceFacade().getWorkflowInstance(wId);
        if (wiModel == null) {
            log.info("业务流程对象不存在");
        }
        Object obj = bizObject.get("xzkhb");
        Map map= JSONObject.parseObject(JSONObject.toJSONBytes(obj), HashMap.class);
        String ids =(String) map.get("id");
        String[] split = ids.split(";");
        IdList idList = new IdList();
        idList.setIds(split);
        idList.setStatus(status);
        assessManagementService.updatePersonAssessmentStatus(idList,status);
        return AjaxResult.success("修改成功");
    }
}
